<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/common.php';

page::printTop();
$page_user_id = $_GET['user_id'];
$page_user = user::getById($page_user_id);
$page_user_publish_item_troop = troop::getUserItemTroop($page_user_id);
$page_user_publish_item_troop_count = troop::getUserItemTroopCount($page_user_id);
$page_user_like_item_troop = troop::getUserLikeItemTroop($page_user_id);
$page_user_like_item_troop_count = troop::getUserLikeItemTroopCount($page_user_id);

$page_user_followees_id_list = bond::getUserFolloweesId($page_user_id);
$page_user_followees_count = count($page_user_followees_id_list);

$page_user_followers_id_list = bond::getUserFollowersId($page_user_id);
$page_user_followers_count = count($page_user_followers_id_list);
?>

<div class="user_page_title">
<?php
	if ($_SESSION['user_id'] && $_SESSION['user_id'] != $page_user_id)
	{
		if (bond::isFollowing($page_user_id))
			echo '<input id="unfollow_user" class="user_page_func_button" type=button value="取消关注TA" /> ->';
		else
			echo '<input id="follow_user" class="user_page_func_button" type=button value="关注TA" /> ->';
	}
?>
	<b><?php echo $page_user['user_nickname'];  ?></b> -> 个人主页
</div>

<div class="user_page_category"><?php echo $page_user['user_nickname'];?> 发表的图片
	<?php echo $page_user_publish_item_troop_count;?>
	张<br />
	<?php
	if (is_array($page_user_publish_item_troop))
	foreach ($page_user_publish_item_troop as $item)
	{
		echo "<div class=user_item>";
	
		echo sprintf("<div id=user_%d_item_%d class=user_item_label>"
			."<input class='user_item_button user_item_button_view' type=button value='查' />"
			."<input class='user_item_button user_item_button_edit' type=button value='改' />"
			."<input class='user_item_button user_item_button_delete' type=button value='删' />"
			."</div>", $item['user_id'], $item['item_id']);
	
		echo sprintf("<img onerror='this.src=\"/images/no_image_avail.jpg\"' src='%s' class=user_item_image />",
			$item['item_image_s']);
		echo "</div>\n";
	}
	?>
</div>

<div class="user_page_category"><?php echo $page_user['user_nickname'];?> 喜欢的图片
	<?php echo $page_user_like_item_troop_count;?>
	张<br />
	<?php
	if (is_array($page_user_like_item_troop))
	foreach ($page_user_like_item_troop as $item)
	{
		echo "<div class=user_item>";
	
		echo sprintf("<div id=user_%d_item_%d class=user_item_label>"
			."<input class='user_item_button user_item_button_view' type=button value='查' />"
			."<input class='user_item_button user_item_button_edit' type=button value='改' />"
			."<input class='user_item_button user_item_button_delete' type=button value='删' />"
			."</div>", $item['user_id'], $item['item_id']);
	
		echo sprintf("<img onerror='this.src=\"/images/no_image_avail.jpg\"' src='%s' class=user_item_image />", $item['item_image_s']);
		echo "</div>\n";
	}
	?>
</div>

<div class="user_page_category"><?php echo $page_user['user_nickname'];?> 关注的用户
	<?php echo $page_user_followees_count;?>
	位<br />
	<?php
	if (is_array($page_user_followees_id_list))
	foreach ($page_user_followees_id_list as $followee_id)
	{
		$followee = user::getById($followee_id);
		if (empty($followee)) continue;
		echo "<div class=followee>";
	
		echo sprintf("<img src='/images/user.png' class=followee_image /> <a href='/user/%d/'>%s</a>", 		$followee['user_id'], $followee['user_nickname']);
		echo "</div>\n";
	}
	?>
</div>

<div class="user_page_category">关注 <?php echo $page_user['user_nickname'];?> 的用户
	<?php echo $page_user_followers_count;?>
	位<br />
	<?php
	if (is_array($page_user_followers_id_list))
	foreach ($page_user_followers_id_list as $follower_id)
	{
		$follower = user::getById($follower_id);
		if (empty($follower)) continue;
		echo "<div class=followee>";
	
		echo sprintf("<img src='/images/user.png' class=followee_image /> <a href='/user/%d/'>%s</a>", 		$follower['user_id'], $follower['user_nickname']);
		echo "</div>\n";
	}
	?>
</div>

<script>
$(function(){
	$('#follow_user').bind('click', function(e) {
		document.location.href = '/user/<?php echo $page_user_id; ?>/follow/';
	});
	
	$('#unfollow_user').bind('click', function(e) {
		document.location.href = '/user/<?php echo $page_user_id; ?>/unfollow/';
	});
	
	$('.user_item').each(function(i, item) {
		var t = $(this).children('.user_item_label').attr('id').split('_');
		var user_id = parseInt(t[1]);
		var item_id = parseInt(t[3]);

		$(item).children('.user_item_image').bind('click', function(e){
			document.location.href = '/item/'+item_id+'/';
		}).css({cursor:'pointer'});;
		
		if (user_id != <?php echo intval($_SESSION['user_id']); ?>)
		{
			return;
		}

		$(item).bind('mouseenter', function(e){
			var cell=$(this).children('.user_item_label');
			cell.show(200);
		});

		$(item).bind('mouseleave', function(e){
			var cell=$(this).children('.user_item_label');
			cell.hide(200);
		});

		$('.user_item_button_view', item).bind('click', function(e){
			document.location.href = '/item/'+item_id+'/';
		});

		$('.user_item_button_edit', item).bind('click', function(e){
			document.location.href = '/item/'+item_id+'/edit/';
		});

		$('.user_item_button_delete', item).bind('click', function(e){
			if (confirm("确认删除？"))
			{
				document.location.href = '/item/'+item_id+'/delete/';
			}
		});
	});

});
</script>

<?php
page::printTail();
